package user

import (
	"errors"
	"hc_server/routers/models"
)

type User struct {
	ID       int
	Ip       string //ip地址
	Password string //密码
	Mobile   string //手机号码
	NickName string //昵称
	RealName string //真实姓名
	Sex      int    //性别: 1=男 2=女
	Avatar   string //头像名称
	Remark   string //备注
}

// ExistByName 通过姓名查询
func (u *User) ExistByName() (bool, error) {
	return models.ExistUserByName(u.NickName)
}

// ExistByID 通过id查询
func (u *User) ExistByID() (bool, error) {
	return models.ExistUserByID(u.ID)
}

// ExistByMobile 通过手机号码查询
func (u *User) ExistByMobile() (bool, error) {
	return models.ExistUserByMobile(u.Mobile)
}

func (u *User) UserInfoByID() (map[string]string, error) {
	return models.UserInfoByID(u.ID)
}

// UserLogin 登录
func (u *User) UserLogin() (map[string]string, error) {
	return models.UserLogin(u.Mobile, u.Password)
}

// UserDelete 删除用户
//func (u *User) UserDelete() error {
//	return models.DeleteTag(u.ID)
//}

// UserAdd 添加用户
func (u *User) UserAdd() (string, error) {
	user := map[string]interface{}{
		"password":  u.Password,
		"mobile":    u.Mobile,
		"nick_name": u.NickName,
		"real_name": u.RealName,
		"sex":       u.Sex,
		"avatar":    u.Avatar,
		"remark":    u.Remark,
	}

	userId, err := models.UserAdd(user)
	if err != nil {
		return "", err
	}
	return userId, nil
}

// UserEdit 编辑昵称
func (u *User) UserEdit(mapd map[string]interface{}) error {
	return models.EditUser(u.ID, mapd)
}

// EditUserNickName 编辑昵称
func (u *User) EditUserNickName() error {
	if len(u.NickName) == 0 {
		return errors.New("缺少参数nick_name")
	}
	return models.EditUser(u.ID, map[string]interface{}{"nick_name": u.NickName})
}

// EditUserMobile 编辑手机号码
func (u *User) EditUserMobile() error {
	if len(u.Mobile) == 0 {
		return errors.New("缺少参数mobile")
	}
	return models.EditUser(u.ID, map[string]interface{}{"mobile": u.Mobile})
}

// EditUserRealName 编辑真实姓名
func (u *User) EditUserRealName() error {
	if len(u.RealName) == 0 {
		return errors.New("缺少参数real_name")
	}
	return models.EditUser(u.ID, map[string]interface{}{"real_name": u.RealName})
}

// EditUserSex 编辑性别
func (u *User) EditUserSex() error {
	data := make(map[string]interface{})
	if u.Sex == 0 {
		return errors.New("缺少参数sex")
	}
	data["sex"] = u.Sex
	return models.EditUser(u.ID, map[string]interface{}{"sex": u.Sex})
}

// EditUserRemark 编辑备注
func (u *User) EditUserRemark() error {
	if len(u.Remark) == 0 {
		return errors.New("缺少参数remark")
	}
	return models.EditUser(u.ID, map[string]interface{}{"remark": u.Remark})
}

// EditUserAvatar 编辑头像
func (u *User) EditUserAvatar() error {
	if len(u.Avatar) == 0 {
		return errors.New("缺少参数avatar")
	}
	return models.EditUser(u.ID, map[string]interface{}{"avatar": u.Avatar})
}

// EditUserPassword 编辑密码
func (u *User) EditUserPassword(nPwd string) error {
	if len(u.Password) == 0 {
		return errors.New("缺少参数password")
	}
	if len(nPwd) == 0 {
		return errors.New("缺少参数nPwd")
	}
	return models.EditUserPassword(u.ID, u.Password, nPwd)
}

// UserAddVerify 添加用户前判断数据完整
func (u *User) UserAddVerify() (bool, error) {
	if len(u.Password) == 0 {
		return false, errors.New("缺少参数Password")
	}
	if len(u.Mobile) == 0 {
		return false, errors.New("缺少参数mobile")
	}
	if len(u.NickName) == 0 {
		return false, errors.New("缺少参数nickName")
	}

	return true, nil
}

// GetUserEditMap 创建sql语句
func (u *User) GetUserEditMap() map[string]interface{} {
	userMap := make(map[string]interface{}, 5)
	if len(u.NickName) > 0 {
		userMap["nick_name"] = u.NickName
	}
	if len(u.RealName) > 0 {
		userMap["real_name"] = u.RealName
	}
	//if len(u.Mobile) > 0 {
	//	userMap["mobile"] = u.Mobile
	//}
	if u.Sex > 0 {
		userMap["sex"] = u.Sex
	}
	if len(u.Remark) > 0 {
		userMap["remark"] = u.Remark
	}
	return userMap
}
